package com.sys.ai.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sys.ai.domain.AiKnowledge;
import com.sys.ai.domain.AiKnowledgeFragmentation;
import com.sys.ai.mapper.AiKnowledgeFragmentationMapper;
import com.sys.ai.service.AiService;
import com.sys.ai.service.IAiKnowledgeFragmentationService;
import com.sys.ai.service.IAiKnowledgeService;
import com.sys.ai.service.IDbOptionService;
import com.sys.ai.vo.EmbeddingsVO;
import com.sys.ai.vo.KnowledgeFragmentationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 知识库分片Service业务层处理
 *
 * @author LGY
 * @date 2025-05-26
 */
@Slf4j
@Service
public class AiKnowledgeFragmentationServiceImpl extends ServiceImpl<AiKnowledgeFragmentationMapper, AiKnowledgeFragmentation> implements IAiKnowledgeFragmentationService {
    @Autowired
    private AiKnowledgeFragmentationMapper aiKnowledgeFragmentationMapper;
    @Autowired
    private IDbOptionService dbOptionService;
    @Autowired
    private AiService aiService;
    @Autowired
    private IAiKnowledgeService aiKnowledgeService;

    /**
     * 查询知识库分片
     *
     * @param id 知识库分片主键
     * @return 知识库分片
     */
    @Override
    public AiKnowledgeFragmentation selectAiKnowledgeFragmentationById(String id) {
        return this.getById(id);
    }

    /**
     * 查询知识库分片列表
     *
     * @param aiKnowledgeFragmentation 知识库分片
     * @return 知识库分片
     */
    @Override
    public List<AiKnowledgeFragmentation> selectAiKnowledgeFragmentationList(AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        return aiKnowledgeFragmentationMapper.selectAiKnowledgeFragmentationList(aiKnowledgeFragmentation);
    }

    /**
     * 新增知识库分片
     *
     * @param aiKnowledgeFragmentation 知识库分片
     * @return 结果
     */

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertAiKnowledgeFragmentation(AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        this.save(aiKnowledgeFragmentation);
    }

    /**
     * 修改知识库分片
     *
     * @param aiKnowledgeFragmentation 知识库分片
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateAiKnowledgeFragmentation(AiKnowledgeFragmentation aiKnowledgeFragmentation) {
        this.updateById(aiKnowledgeFragmentation);
    }

    /**
     * 批量删除知识库分片
     *
     * @param ids 需要删除的知识库分片主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteAiKnowledgeFragmentationByIds(String[] ids) {
        this.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBatch(List<String> knowledgeIds) {
        new LambdaUpdateChainWrapper<>(baseMapper)
                .in(AiKnowledgeFragmentation::getKnowledgeId, knowledgeIds)
                .remove();
    }

    @Override
    public List<AiKnowledgeFragmentation> listAllByKnowledgeId(String knowledgeId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(AiKnowledgeFragmentation::getKnowledgeId, knowledgeId)
                .list();
    }

    @Override
    public List<AiKnowledgeFragmentation> listAllByKnowledgeFileId(String knowledgeFileId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(AiKnowledgeFragmentation::getAiKnowledgeFile, knowledgeFileId)
                .list();
    }

    @Override
    public String queryFragmentationBySearch(String knowledgeId, String search) {
        AiKnowledge aiKnowledge = aiKnowledgeService.getById(knowledgeId);
        EmbeddingsVO embeddingsVO = aiService.generateQAToEmbeddings(search, aiKnowledge.getRankModelId());
        List<KnowledgeFragmentationVO> knowledgeFragmentationVOS = dbOptionService.query(embeddingsVO.getData().get(0).getEmbedding(), List.of(knowledgeId));
        StringBuilder stringBuilder = new StringBuilder();
        if (!CollectionUtils.isEmpty(knowledgeFragmentationVOS)) {
            knowledgeFragmentationVOS.forEach(item -> {
                stringBuilder.append(item.getFragmentation()).append("\n");
            });
        }
        return stringBuilder.toString();
    }
}